home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 3 / AGA Experience Volume 3 (1997)(NFA - SAdENESS)[!].iso / software / utilities / demos / ged451 / install.run / GOLDEDDATA / developer / examples / scanner / source / html.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-07  |  5.1 KB  |  160 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   COPYIGHT
  4.  
  5.   ©1995  Dietmar  Eilert  (e-mail:  DIETMAR@TOMATE.TNG.OCHE.DE).  All  Rights
  6.   Reserved.  Code  may not be reused/reproduced without written permission of
  7.   the author.
  8.  
  9.   Dietmar Eilert
  10.   Mies-v-d-Rohe-Str.31, 52074 Aachen, Germany
  11.   E-Mail: DIETMAR@TOMATE.TNG.OCHE.DE
  12.   Tel: +49-(0)241-81665
  13.        +49-(0)2525-7776
  14.   Fax: +49-(0)241-81665
  15.  
  16.   Example: scan handler looking for HTML anchors. Known bug: can not handle
  17.   more than one anchor per line due to the scanner interface design.
  18.  
  19.   Scan handlers are plain functions (LoadSeg'ed by GED): no standard C startup
  20.   code, no library calls. String constants have to be put into the code chunk.
  21.   That's why below a string constant is (ab)used as a buffer instead of using
  22.   a static array.
  23.  
  24.   DICE-C:
  25.  
  26.   dcc html.c -// -l0 -md -ms2 -mRR -o ram:HTML
  27.  
  28.   ------------------------------------------------------------------------------
  29. */
  30.  
  31. #include <exec/types.h>
  32.  
  33. #define UPPER(a) ((a) & 95)
  34.  
  35. ULONG
  36. ScanHandlerHTML(__D0 ULONG len, __A0 char **text, __A1 ULONG *line)
  37. {
  38.     const char *version = "$VER: HTML 1.2 (" __COMMODORE_DATE__ ")";
  39.  
  40.     // use string constant as result buffer (string constants go into the code section; remember: this program is compiled without startup code)
  41.  
  42.     UBYTE *buffer = "                                                       ";
  43.  
  44.     // minimum strings: <A NAME="X"> or <A HREF="X">
  45.  
  46.     if (len >= 12) {
  47.  
  48.         UBYTE *from, *start, *last;
  49.  
  50.         from = *text;
  51.         last = *text + len;
  52.  
  53.         while ((from + 11) < last) {
  54.  
  55.             // anchor detected ?
  56.  
  57.             if ((from[0] == '<') && (UPPER(from[1]) == 'A') && (from[2] == ' ')) {
  58.  
  59.                 // 1st try: look for <A HREF="..."> or <A NAME="...">
  60.  
  61.                 for (start = from + 3; start < last; ++start) {
  62.  
  63.                     if (UPPER(*start) == 'H') {
  64.  
  65.                         if (UPPER(start[1]) == 'R') {
  66.  
  67.                             if (UPPER(start[2]) == 'E') {
  68.  
  69.                                 if (UPPER(start[3]) == 'F') {
  70.  
  71.                                     for (start += 4; start < last; ++start) {
  72.  
  73.                                         // look for beginning of link string
  74.  
  75.                                         if (*start == 34) {
  76.  
  77.                                             UBYTE *result = buffer;
  78.  
  79.                                             *result++ = 'H';
  80.                                             *result++ = 'R';
  81.                                             *result++ = 'E';
  82.                                             *result++ = 'F';
  83.                                             *result++ = ' ';
  84.  
  85.                                             for (len = 5, ++start; start < last; ++len) {
  86.  
  87.                                                 if (len == 50)
  88.  
  89.                                                     break;
  90.  
  91.                                                 if (*start == 34) {
  92.  
  93.                                                     *text = buffer;
  94.  
  95.                                                     return(len);
  96.                                                 }
  97.                                                 else
  98.                                                     *result++ = *start++;
  99.                                             }
  100.                                         }
  101.                                     }
  102.                                 }
  103.                             }
  104.                         }
  105.                     }
  106.  
  107.                     if (UPPER(*start) == 'N') {
  108.  
  109.                         if (UPPER(start[1]) == 'A') {
  110.  
  111.                             if (UPPER(start[2]) == 'M') {
  112.  
  113.                                 if (UPPER(start[3]) == 'E') {
  114.  
  115.                                     for (start += 4; start < last; ++start) {
  116.  
  117.                                         // look for beginning of link string
  118.  
  119.                                         if (*start == 34) {
  120.  
  121.                                             UBYTE *result = buffer;
  122.  
  123.                                             *result++ = 'N';
  124.                                             *result++ = 'A';
  125.                                             *result++ = 'M';
  126.                                             *result++ = 'E';
  127.                                             *result++ = ' ';
  128.  
  129.                                             for (len = 5, ++start; start < last; ++len) {
  130.  
  131.                                                 if (len == 50)
  132.  
  133.                                                     break;
  134.  
  135.                                                 if (*start == 34) {
  136.  
  137.                                                     *text = buffer;
  138.  
  139.                                                     return(len);
  140.                                                 }
  141.                                                 else
  142.                                                     *result++ = *start++;
  143.                                             }
  144.                                         }
  145.                                     }
  146.                                 }
  147.                             }
  148.                         }
  149.                     }
  150.                 }
  151.             }
  152.  
  153.             ++from;
  154.         }
  155.     }
  156.  
  157.     return(FALSE);
  158. }
  159.  
  160.